using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using JGSY.CMS.LowCode.Platform.Domain.Entities.Base;

namespace JGSY.CMS.LowCode.Platform.Domain.Entities
{
    /// <summary>
    /// 用户管理实体
    /// 对应 users 表，支持部门组织架构和完整用户信息，支持多租户隔离
    /// </summary>
    [Table("users")]
    public class User : AuditableEntity, ITenantEntity
    {
        /// <summary>
        /// 租户ID
        /// 用于多租户数据隔离
        /// </summary>
        [Required]
        [StringLength(36)]
        public string TenantId { get; set; } = string.Empty;

        /// <summary>
        /// 用户名
        /// </summary>
        [Required]
        [StringLength(50)]
        public string Username { get; set; } = string.Empty;
        
        /// <summary>
        /// 密码哈希
        /// </summary>
        [Required]
        [StringLength(255)]
        public string PasswordHash { get; set; } = string.Empty;
        
        /// <summary>
        /// 邮箱
        /// </summary>
        [StringLength(100)]
        public string? Email { get; set; }
        
        /// <summary>
        /// 手机号
        /// </summary>
        [StringLength(20)]
        public string? Phone { get; set; }

        /// <summary>
        /// 真实姓名
        /// </summary>
        [StringLength(100)]
        public string? RealName { get; set; }

        /// <summary>
        /// 头像URL
        /// </summary>
        [StringLength(255)]
        public string? Avatar { get; set; }

        /// <summary>
        /// 状态（启用/禁用）
        /// 0=禁用, 1=启用
        /// </summary>
        public byte Status { get; set; } = 1;

        /// <summary>
        /// 最后登录IP
        /// </summary>
        [StringLength(50)]
        public string? LastLoginIp { get; set; }

        /// <summary>
        /// 最后登录时间
        /// </summary>
        public DateTime? LastLoginTime { get; set; }

        /// <summary>
        /// 部门ID
        /// </summary>
        public long? DepartmentId { get; set; }

        /// <summary>
        /// 社交媒体OpenID
        /// 用于第三方登录的OpenID
        /// </summary>
        [StringLength(255)]
        public string? OpenId { get; set; }

        /// <summary>
        /// 昵称
        /// 用户显示昵称
        /// </summary>
        [StringLength(100)]
        public string? Nickname { get; set; }

        /// <summary>
        /// 第三方登录提供商
        /// 如：wechat, alipay, qq等
        /// </summary>
        [StringLength(50)]
        public string? Provider { get; set; }

        /// <summary>
        /// 用户级别/等级
        /// </summary>
        public int Level { get; set; } = 1;

        /// <summary>
        /// 经验值/积分
        /// </summary>
        public int Experience { get; set; } = 0;

        /// <summary>
        /// 职位/岗位
        /// </summary>
        [StringLength(100)]
        public string? Position { get; set; }

        /// <summary>
        /// 工号
        /// </summary>
        [StringLength(50)]
        public string? EmployeeNumber { get; set; }

        /// <summary>
        /// 入职时间
        /// </summary>
        public DateTime? HiredAt { get; set; }

        /// <summary>
        /// 生日
        /// </summary>
        public DateTime? Birthday { get; set; }

        /// <summary>
        /// 性别（0=未知, 1=男, 2=女）
        /// </summary>
        public byte Gender { get; set; } = 0;

        /// <summary>
        /// 地址
        /// </summary>
        [StringLength(500)]
        public string? Address { get; set; }

        /// <summary>
        /// 紧急联系人
        /// </summary>
        [StringLength(100)]
        public string? EmergencyContact { get; set; }

        /// <summary>
        /// 紧急联系电话
        /// </summary>
        [StringLength(20)]
        public string? EmergencyPhone { get; set; }

        /// <summary>
        /// 备注信息
        /// </summary>
        [Column(TypeName = "text")]
        public string? Remark { get; set; }

        /// <summary>
        /// 个人设置（JSON格式）
        /// </summary>
        [Column(TypeName = "json")]
        public string? Settings { get; set; }

        // 导航属性

        /// <summary>
        /// 租户导航属性
        /// </summary>
        public Tenant? Tenant { get; set; }

        /// <summary>
        /// 部门导航属性
        /// </summary>
        [ForeignKey(nameof(DepartmentId))]
        public Department? Department { get; set; }

        /// <summary>
        /// 用户角色关联集合
        /// </summary>
        [InverseProperty(nameof(UserRole.User))]
        public ICollection<UserRole> UserRoles { get; set; } = new List<UserRole>();

        /// <summary>
        /// 用户设备集合
        /// </summary>
        [InverseProperty(nameof(UserDevice.User))]
        public ICollection<UserDevice> Devices { get; set; } = new List<UserDevice>();

        /// <summary>
        /// 创建的页面集合
        /// </summary>
        [InverseProperty(nameof(Page.Creator))]
        public ICollection<Page> CreatedPages { get; set; } = new List<Page>();

        /// <summary>
        /// 创建的文章集合
        /// </summary>
        [InverseProperty(nameof(Article.Creator))]
        public ICollection<Article> CreatedArticles { get; set; } = new List<Article>();

        /// <summary>
        /// 发布的文章集合
        /// </summary>
        [InverseProperty(nameof(Article.Publisher))]
        public ICollection<Article> PublishedArticles { get; set; } = new List<Article>();

        /// <summary>
        /// 创建的产品集合
        /// </summary>
        [InverseProperty(nameof(Product.Creator))]
        public ICollection<Product> CreatedProducts { get; set; } = new List<Product>();

        /// <summary>
        /// 上传的媒体文件集合
        /// </summary>
        [InverseProperty(nameof(MediaFile.Creator))]
        public ICollection<MediaFile> UploadedFiles { get; set; } = new List<MediaFile>();

        /// <summary>
        /// 创建的表单集合
        /// </summary>
        [InverseProperty(nameof(Form.Creator))]
        public ICollection<Form> CreatedForms { get; set; } = new List<Form>();

        /// <summary>
        /// 提交的表单数据集合
        /// </summary>
        [InverseProperty(nameof(FormSubmission.Submitter))]
        public ICollection<FormSubmission> FormSubmissions { get; set; } = new List<FormSubmission>();

        /// <summary>
        /// 团队成员关联集合
        /// </summary>
        [InverseProperty(nameof(TeamMember.User))]
        public ICollection<TeamMember> TeamMemberships { get; set; } = new List<TeamMember>();

        /// <summary>
        /// 领导的团队集合
        /// </summary>
        [InverseProperty(nameof(Team.Leader))]
        public ICollection<Team> LeadingTeams { get; set; } = new List<Team>();

        /// <summary>
        /// 创建的团队集合
        /// </summary>
        [InverseProperty(nameof(Team.Creator))]
        public ICollection<Team> CreatedTeams { get; set; } = new List<Team>();
    }
}
